package com.blog.admin.controller;


import com.blog.admin.service.SysUserService;
import com.blog.admin.vo.SysUserVO;
import com.blog.vo.VerifyCodeVO;
import com.blog.common.SimpleCharVerifyCodeGenService;
import com.blog.util.PasswordUtil;
import com.blog.util.Result;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登录模块
 */
@Controller
@Slf4j
@RequestMapping("loginController")
public class LoginController  {

    @Autowired
    SysUserService sysUserService;
    @Autowired
    SimpleCharVerifyCodeGenService simpleCharVerifyCodeGenService;

    /**
     * 后台登录接口
     *
     * @return
     */
    @RequestMapping("login")
    @ResponseBody
    public JSONObject login(SysUserVO sysUserVO, HttpServletRequest request)  {
        try {
            String VerifyCode1 = (String) request.getSession().getAttribute("VerifyCode");
            String VerifyCode =  request.getParameter("VerifyCode");
            System.out.println(VerifyCode1+"==>"+VerifyCode);
            if (StringUtils.isEmpty(VerifyCode1)){
                return Result.responeAjax(false, "验证码不能为空", null);
            }
            if (!VerifyCode.toLowerCase().equals(VerifyCode1.toLowerCase())){
                return Result.responeAjax(false, "验证码错误", null);
            }

            sysUserVO.setPassword(PasswordUtil.encrypt(sysUserVO.getPassword(),sysUserVO.getUsername(),PasswordUtil.getStaticSalt()));
            //shiro登录 跳转至MyShiroRealm 进行校验
            UsernamePasswordToken token = new UsernamePasswordToken(sysUserVO.getUsername(), sysUserVO.getPassword());
            SecurityUtils.getSubject().login(token);
            SysUserVO user =  (SysUserVO)SecurityUtils.getSubject().getPrincipal();
            request.getSession().setAttribute("adminUser",user);
        } catch (Exception e) {
            return Result.responeAjax(false, e.getMessage(), null);
        }
        return Result.responeAjax(true, "成功", null);
    }

    /**
     * 新增用户
     *
     * @return
     */
    /*@RequestMapping("register")
    @ResponseBody
    public JSONObject register(SysUserVO sysUserVO, SysUserInfomationVO sysUserInfomationVO) {
        SysUserVO loginuser  =  sysUserService.getUserByUsername(sysUserVO.getUsername());
        if (loginuser!=null){
            return Result.responeAjax(false, "账号已存在", null);
        }
        // 密码进行md5加密
        sysUserVO.setPassword(PasswordUtil.encrypt(sysUserVO.getPassword(),sysUserVO.getUsername(),PasswordUtil.getStaticSalt()));
        SysRoleVO sysRoleVO = new SysRoleVO();
        //赋予默认id
        sysRoleVO.setId("e6d2f46939c611ea8e185254009a012e");
        sysUserVO.setSysRoleVO(sysRoleVO);
        boolean result = sysUserService.insertUser(sysUserVO,sysUserInfomationVO );
        return Result.responeAjax(result, "成功", null);
    }*/

    @GetMapping("/verifyCode")
    public void verifyCode(HttpServletRequest request, HttpServletResponse response) {
        try {
            //设置长宽
            VerifyCodeVO verifyCode = simpleCharVerifyCodeGenService.generate(80, 28);
            String code = verifyCode.getCode();
            log.info(code);
            //设置响应头
            response.setHeader("Pragma", "no-cache");
            //设置响应头
            response.setHeader("Cache-Control", "no-cache");
            //在代理服务器端防止缓冲
            response.setDateHeader("Expires", 0);
            //设置响应内容类型
            response.setContentType("image/jpeg");
            response.getOutputStream().write(verifyCode.getImgBytes());
            //将VerifyCode绑定session
            request.getSession().setAttribute("VerifyCode", code);
            response.getOutputStream().flush();
        } catch (IOException e) {
            log.info("", e);
        }
    }

}
